package com.tensquare.search.controller;

import com.tensquare.search.pojo.ArticleSearch;
import com.tensquare.search.service.ArticleSearchService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description：文章搜索控制层
 * @Author: liulei
 * @Date: 2019年10月27日
 * @Version: V1.0
 **/
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleSearchController {

    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 文章搜索，添加
     * @param articleSearch
     * @return
     */
    @PostMapping
    public Result save(@RequestBody ArticleSearch articleSearch){
        articleSearchService.save(articleSearch);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 根据关键字搜索+分页
     * @param key
     * @param size
     * @param page
     * @return
     */
    @GetMapping("/{keyword}/{size}/{page}")
    public Result findByKey(@PathVariable String keyword, @PathVariable int page, @PathVariable int size){
        Page<ArticleSearch> pageData = articleSearchService.findByKey(keyword, page, size);
        long total = pageData.getTotalElements();
        List<ArticleSearch> list = pageData.getContent();
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<>(total, list));
    }
}
